package xml;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.net.MalformedURLException;
import java.net.URL;

/**
 * @ClassName: XmlbyDom4j
 * @Description: TODO
 * @author xiaoming
 * 2018年6月18日下午2:36:53
 */
public class XmlbyDom4j {
	public static void main(String[] args) throws MalformedURLException, DocumentException {
		XmlbyDom4j dom = new XmlbyDom4j();
		dom.parseXMLDocument();
	}
	
	public void parseXMLDocument() throws DocumentException, MalformedURLException {
		    URL url = new URL("https://www.zhihu.com/rss");
			
			SAXReader reader = new SAXReader();
			Document document = reader.read(url);
			System.out.println(document.asXML());
	}
	
	public void createXMLDocument() {
		// 创建document对象
		Document document = DocumentHelper.createDocument();
		// 根节点
		Element root = document.addElement("root");
		// 添加属性
		root.addAttribute("id", "root");
		// 添加子节点
		Element head = root.addElement("head");
		Element body = root.addElement("body");
		// 添加内容
		head.setText("hello world");
		body.setText("hello body");
		// 增加内容 ( 类似StringBuilder
		head.addText(",th is head");
		// 修改内容(再次set
		body.setText("body内容已修改");
		// 清除节点内容
		body.clearContent();
		// 删除节点
		// root.remove(body);
		System.out.println(document.asXML());
	};
}
